package com.wanxs.mall.product.controller;

import com.wanxs.mall.common.annotation.LogOperation;
import com.wanxs.mall.common.constant.Constant;
import com.wanxs.mall.common.page.PageData;
import com.wanxs.mall.common.utils.ExcelUtils;
import com.wanxs.mall.common.utils.Result;
import com.wanxs.mall.common.validator.AssertUtils;
import com.wanxs.mall.common.validator.ValidatorUtils;
import com.wanxs.mall.common.validator.group.AddGroup;
import com.wanxs.mall.common.validator.group.DefaultGroup;
import com.wanxs.mall.common.validator.group.UpdateGroup;
import com.wanxs.mall.product.dto.AttrDTO;
import com.wanxs.mall.product.entity.AttrAttrgroupRelationEntity;
import com.wanxs.mall.product.entity.AttrEntity;
import com.wanxs.mall.product.excel.AttrExcel;
import com.wanxs.mall.product.service.AttrAttrgroupRelationService;
import com.wanxs.mall.product.service.AttrGroupService;
import com.wanxs.mall.product.service.AttrService;
import com.wanxs.mall.product.service.CategoryService;
import com.wanxs.mall.product.vo.AttrResponseVo;
import com.wanxs.mall.product.vo.AttrVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 商品属性  规格参数
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-01
 */
@RestController
@RequestMapping("product/attr")
@Api(tags="商品属性")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("product:attr:page")
    public Result<PageData<AttrDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AttrDTO> page = attrService.page(params);

        return new Result<PageData<AttrDTO>>().ok(page);
    }

    @GetMapping("info/{id}")
    @ApiOperation("信息")
    @RequiresPermissions("product:attr:info")
    public Result<AttrVO> get(@PathVariable("id") Long id){
        AttrVO attrVO = new AttrVO();
        AttrEntity attrEntity = attrService.selectById(id);
        AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = attrAttrgroupRelationService.selectOne(attrEntity.getAttrId());
        BeanUtils.copyProperties(attrEntity,attrVO);
        attrVO.setAttrGroupId(attrAttrgroupRelationEntity.getAttrGroupId());
        Long[] paths = categoryService.getCatelogPath(attrEntity.getCatelogId());
        attrVO.setCatelogPath(paths);
        return new Result<AttrVO>().ok(attrVO);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
//    @RequiresPermissions("product:attr:save")
    public Result save(@RequestBody AttrVO attrVO){
        //效验数据
        ValidatorUtils.validateEntity(attrVO, AddGroup.class, DefaultGroup.class);

        attrService.saveAttr(attrVO);

        return new Result();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("product:attr:update")
    public Result update(@RequestBody AttrVO attrVO){
        //效验数据
        ValidatorUtils.validateEntity(attrVO, UpdateGroup.class, DefaultGroup.class);

        attrService.updateBaseAttr(attrVO);

        return new Result();
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    @LogOperation("删除")
//    @RequiresPermissions("product:attr:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        attrService.deleteAttr(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("product:attr:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AttrDTO> list = attrService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, AttrExcel.class);
    }




//
//    @GetMapping("base/list/{catId}")
//    public Result<PageData<AttrDTO>> basePage(@ApiIgnore @RequestParam Map<String, Object> params
//    ,@PathVariable("catId") Long catId){
//        PageData<AttrDTO> page = attrService.page(params);
//
//        return new Result<PageData<AttrDTO>>().ok(page);
//    }
//
//
//    @GetMapping("sale/list/{catId}")
//    public Result<PageData<AttrDTO>> salePage(@ApiIgnore @RequestParam Map<String, Object> params
//            ,@PathVariable("catId") Long catId){
//        PageData<AttrDTO> page = attrService.page(params);
//
//        return new Result<PageData<AttrDTO>>().ok(page);
//    }


    @GetMapping("/{attrType}/list/{catelogId}")
    public Result<PageData<AttrResponseVo>> baseList(@RequestParam Map<String, Object> params
            ,@PathVariable("catelogId") Long catelogId
            ,@PathVariable("attrType") String attrType
    ){
        PageData<AttrResponseVo> page = attrService.queryBasePage(params,catelogId,attrType);

        return new Result<PageData<AttrResponseVo>>().ok(page);
    }


    @GetMapping("{attrGroupId}/noattr/relation")
    public Result<PageData<AttrEntity>> getNoAttrRelation(@ApiIgnore @RequestParam Map<String, Object> params
            , @PathVariable("attrGroupId") Long attrGroupId){
        PageData<AttrEntity> page = attrService.getNoAttrRelation(params,attrGroupId);

        return new Result<PageData<AttrEntity>>().ok(page);
    }
}